/**
 * By Charles
 */

$(function(){
	var urlStr=location.search;
	 var parObj = new Object();
	 if (urlStr.indexOf("?") != -1) {
	     urlPara = urlStr.substr(urlStr.indexOf("?") + 1).split("&");
	 }
	 for (var i = 0; i < urlPara.length; i++) {
	     parObj[urlPara[i].split("=")[0].toString()] = urlPara[i].split("=")[1].toString();
	 }
	var vmMain=new Vue({
		el:"#contentBody",
		data:{
			downCourseLis:[],
			downloadFileLis:[],
		},
		methods:{
			downloadFile:function(filePath){
				try{
				   var items=filePath.split("\\");
				   var dname=items[items.length-1].split(".");
				   if(dname[dname.length-1]=="txt"){
					   var test=window.open(filePath,"","height=1px,width=1px,left=0,top=0,scrollbars=no,toolbars=no");//这个方法就直接把这个TXT以浏览器的方式打开了 
					   test.document.execCommand("SaveAs");
					   test.close();
				   }
				   else
				   {
					   var elemIF = document.createElement("iframe");   
			           elemIF.src = filePath;   
			           elemIF.style.display = "none";   
			           document.body.appendChild(elemIF);
				   }
				}
				   catch(e) 
				   { 
					   window.confirm(e.message);//window.confirm("下载完成."); 
				   } 
			},
		}
	});
	$.ajax({
	         type: "post",
	         url: "findMyDownLoadSubject",
	         dataType: "json",
	         async:false,
	         data: {},
	         success: function (result) {
	         	if(result)
	         	{
	         		if((parObj["subjectId"]==undefined||parObj["subjectId"]=="")&&result.length>0)
	         			result[0].isCurrent=true;
	         		else
	         		{
	         			for(var i=0;i<result.length;i++)
		         		{
		         			if(result[i].subjectId==parObj["subjectId"]){
		         				result[i].isCurrent=true;
		         			}
		         			else
		         				result[i].isCurrent=false;	
		         		}
	         		}
	         		vmMain.downCourseLis=result;
	         	}
	         },
	         error: function () {
	             alert("data request errror!");
	             return;
	         }
	});
	var subjectId="";
	if((parObj["subjectId"]==undefined||parObj["subjectId"]=="")&&vmMain.downCourseLis.length>0)
	{
		subjectId=vmMain.downCourseLis[0].subjectId;
	}
	else
		subjectId=parObj["subjectId"];
	if(subjectId!=="")
	{
		$.ajax({
	        type: "post",
	        url: "findMyDownLoadFile",
	        dataType: "json",
	        async:false,
	        data: { subjectId: subjectId},
	        success: function (result) {
	        	if(result)
	        	{
	        		for(var i=0;i<result.length;i++)
	        		{
	        			var fileStr=result[i].filePath.split('.');
	        			if(fileStr[fileStr.length-1]=="pptx"||fileStr[fileStr.length-1]=="ppt")
	        			{
	        				result[i].pType=1;
	        				result[i].bgImg="wd2.jpg";
	        			}
	        			else
	        			{
	        				result[i].pType=2;
	        				if(fileStr[fileStr.length-1]=="docx"||fileStr[fileStr.length-1]=="doc")
	        					result[i].bgImg="wd1.jpg";
	        				else
	        					result[i].bgImg="undefined.png";
	        			}
	        		}
	        		vmMain.downloadFileLis=result;
	        	}
	        	else
	        		vmMain.downloadFileLis=[];
	        },
	        error: function () {
	            alert("data request errror!");
	            return;
	        }
	});
	}
});
